package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.deuwise.system.entity.SysSiteLayer;
import com.deuwise.system.mapper.SysSiteLayerMapper;
import com.deuwise.system.service.SysSiteLayerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright
 *
 * @author chenwenchao
 * @date 2019/9/30
 * Description:
 */
@Service
public class SysSiteLayerServiceImpl extends ServiceImpl<SysSiteLayerMapper, SysSiteLayer> implements SysSiteLayerService {
    @Override
    public List<String> queryLayerList(String siteId, String category) {
        Wrapper<SysSiteLayer> sysRoleMenuWrapper=new EntityWrapper<>();
        sysRoleMenuWrapper.eq("site_id",siteId);
        sysRoleMenuWrapper.addFilter("layer_id is not null");
        if(StringUtils.isNotBlank(category)){
            sysRoleMenuWrapper.eq("category",category);
        }
        List<SysSiteLayer> list=this.selectList(sysRoleMenuWrapper);
        if(list!=null&&list.size()>0){
            return list.stream().map(f->{
                return  f.getLayerId();
            }).collect(Collectors.toList());
        }else{
            return  null;
        }
    }

    /**
     * 获取地图视图配置ID
     * @param siteId 系统id
     * @param category
     * @return
     */
    @Override
    public String queryMapViewBySiteId(String siteId, String category) {
        Wrapper<SysSiteLayer> wrapper=new EntityWrapper<>();
        wrapper.eq("site_id",siteId);
        wrapper.addFilter("layer_id is null");
        if(StringUtils.isNotBlank(category)){
            wrapper.eq("category",category);
        }
        SysSiteLayer siteLayer = this.selectOne(wrapper);
        if(null != siteLayer){
            return siteLayer.getMapViewId();
        }
        return "";
    }

    @Override
    public void saveOrUpdate(String category,String siteId, List<String> layerList, String mapViewId) {
        //先删除角色与菜单关系
        Wrapper<SysSiteLayer> wrapper=new EntityWrapper<>();
        wrapper.eq("site_id",siteId);
        wrapper.eq("category",category);
        wrapper.addFilter("layer_id is not null");
        baseMapper.delete(wrapper);

        if(StringUtils.isNotBlank(mapViewId)){
            //先删除视图配置关联
            wrapper = new EntityWrapper<>();
            wrapper.eq("map_view_id",mapViewId);
            wrapper.addFilter("layer_id is null");
            baseMapper.delete(wrapper);
        }

        if(StringUtils.isNotBlank(mapViewId)){
            SysSiteLayer siteLayer = new SysSiteLayer();
            siteLayer.setMapViewId(mapViewId);
            siteLayer.setSiteId(siteId);
            siteLayer.setCategory(category);
            this.insert(siteLayer);
        }

        if(layerList !=null && layerList.size() > 0){
            //保存系统与站点关系
            List<SysSiteLayer> list = new ArrayList<>(layerList.size());
            for(String menuId : layerList){
                SysSiteLayer sysRoleMenuEntity = new SysSiteLayer();
                sysRoleMenuEntity.setLayerId(menuId);
                sysRoleMenuEntity.setSiteId(siteId);
                sysRoleMenuEntity.setCategory(category);
                list.add(sysRoleMenuEntity);
            }
            this.insertBatch(list);
        }
    }
}
